import message from './message';
import storage from './storage';

export const copyText = (text: string) => {
  if (!text) return;
  // navigator.clipboard.writeText(text).then(() => {
  //   message.success('複製成功');
  // });

  const oInput = document.createElement('input');
  oInput.value = text;
  document.body.appendChild(oInput);
  oInput.select(); // 选择对象;
  document.execCommand('Copy'); // 执行浏览器复制命令
  message.success('复制成功');
  oInput.remove();
};

const isInViewport = (element: HTMLElement): boolean => {
  const rect = element.getBoundingClientRect();
  return (
    rect.top >= 0 &&
    rect.left >= 0 &&
    rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
    rect.right <= (window.innerWidth || document.documentElement.clientWidth)
  );
};

const u = navigator.userAgent;

export const isWeChatEnvironment = () => {
  return u.includes('MicroMessenger');
};

// Android终端
export const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
// IOS 终端
export const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
//  mac
export const isMac = /macintosh|mac os x/i.test(navigator.userAgent);
// windows
export const isWin = /windows|win32/i.test(navigator.userAgent);

export const isAPPEnv = (): boolean => {
  const isApp = storage.get('', 'isApp');
  return Boolean(isApp === '1');
};

const isIpadFun = (): boolean => {
  return !!(
    navigator.maxTouchPoints &&
    navigator.maxTouchPoints > 2 &&
    /MacIntel/.test(navigator.platform)
  );
};

export const isMobile = (): boolean =>
  isAPPEnv() ||
  isIpadFun() ||
  navigator.userAgent
    .toLowerCase()
    .match(
      /(ipod|ipad|iphone|android|coolpad|mmp|smartphone|midp|wap|xoom|symbian|j2me|blackberry|wince)/i,
    ) !== null;

export const sortList = (list: any[], index1: number, index2: number): any[] => {
  const res: any[] = [];
  const active = list[index1];
  if (index1 < index2) {
    list.forEach((v, i) => {
      if (i < index1 || i > index2) {
        res.push(v);
      } else if (i >= index1 && i !== index2) {
        res.push(list[i + 1]);
      } else {
        res.push(active);
      }
    });
  } else if (index1 > index2) {
    list.forEach((v, i) => {
      if (i > index1 || i < index2) {
        res.push(v);
      } else if (i === index2) {
        res.push(active);
      } else {
        res.push(list[i - 1]);
      }
    });
  }
  return res;
};

export const jsFileToBase64 = (file: File) => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = () => {
      resolve(reader.result);
    };
    reader.onerror = reject;
    reader.readAsDataURL(file);
  });
};

export const cutString = (str: string, maxLength: number): string => {
  let length = 0;
  let cutStr = '';
  for (let i = 0; i < str.length; i++) {
    if (str.charCodeAt(i) > 255 || str.charAt(i) === ' ') {
      length += 2;
    } else {
      length++;
    }
    if (length <= maxLength) {
      cutStr += str.charAt(i);
    }
  }
  return cutStr;
};

export const SecretKey = 'tstbbNstFpcih6Day0e2QjJuUbKjNfPpcFTH7OT1ktYoffnCfDMknJMI3eCFPr4f';

export const findPathById = (tree: any, id: number, path: any[] = []): number[] | null => {
  for (const node of tree) {
    const currentPath: any[] = [...path, node.id];

    if (node.id === id) {
      return currentPath;
    }

    if (node.children) {
      const foundPath = findPathById(node.children, id, currentPath);
      if (foundPath) {
        return foundPath;
      }
    }
  }
  return null;
};

export default isInViewport;
